﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;
using System.Xml.Linq;

namespace Framework.WebmasterTool
{
    public partial class Watermark : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadWatermark();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.Watermark.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void LoadWatermark()
        {
            XDocument xDocWatermark = XDocument.Load(Server.MapPath(ConfigPaths.Path_Watermark_Xml));
            XElement xEImage = xDocWatermark.Descendants("img").FirstOrDefault();
            if (xEImage.Value.Trim() != "")
            {
                string sourceImageFilePath = ConfigPaths.Path_ImageUpload_Folder + xEImage.Value.Trim();
                imgSignature.ImageUrl = sourceImageFilePath;
                lblMsg.Text = "Đã có chữ ký ảnh";
                lblMsg.ForeColor = System.Drawing.Color.Blue;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (fulImage.HasFile == false)
            {
                lblMsg.Text = "Chưa upload ảnh cho chữ ký ảnh";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                XDocument xDocWatermark = XDocument.Load(Server.MapPath(ConfigPaths.Path_Watermark_Xml));
                XElement xEImage = xDocWatermark.Descendants("img").FirstOrDefault();
                if (Globals.CheckExtensionImage(fulImage.FileName))
                {
                    Globals.DelelteFile(Server.MapPath(ConfigPaths.Path_ImageUpload_Folder_MapPath + xEImage.Value.Trim()));
                    string fileNameImage = Globals.GetFileName(fulImage.FileName);
                    fulImage.SaveAs(Server.MapPath(ConfigPaths.Path_ImageUpload_Folder_MapPath + fileNameImage));
                    xEImage.Value = fileNameImage;
                    xDocWatermark.Save(Server.MapPath(ConfigPaths.Path_Watermark_Xml));
                    lblMsg.Text = "Upload chữa ký ảnh thành công";
                    lblMsg.ForeColor = System.Drawing.Color.Blue;
                    LoadWatermark();

                    //create db log
                    CreateDBLog(null, null, TypeAction.Update, "Cập nhật chữ ký ảnh", "", "", "", "",
                            "", "", "", "");
                }
                else
                {
                    lblMsg.Text = "File ảnh không đúng định dạng";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}